<?php defined('SYSPATH') OR die('No direct access allowed.');

class Invite_Model extends ORM 
{
	/**
	 * Validates and optionally saves a new invite record from an array.
	 *
	 * @param  array    values to check
	 * @param  boolean  save the record when validation succeeds
	 * @return boolean
	 */
	public function validate(array & $array, $save = FALSE)
	{
		$array = Validation::factory($array)
			->pre_filter('trim')
			->add_rules('user_id', 'required', array($this, 'user_id_exists'))
			->add_rules('invited_user_id', 'required', array($this, 'user_id_exists'));

		return parent::validate($array, $save);
	}

	public function user_id_exists($id)
	{
		echo $id;
		return !$this->unique_key_exists($id);
	}

	public function unique_key_exists($value)
	{
		return (bool) $this->db
			->where('id', $value)
			->count_records('users');
	}

	public function save()
	{
		if ($this->loaded === FALSE)
		{
			// Set the created time, token
			$this->create_time = time();
		}

		return parent::save();
	}

	
} // End Invite Model


